/*
 * Name: $RCSfile: AsyncHttpBase.java,v $
 * Version: $Revision: 1.1 $
 * Date: $Date: Dec 2, 2011 11:06:29 AM $
 *
 * Copyright (C) 2011 COMPANY_NAME, Inc. All rights reserved.
 */

package son.hut.vn.app.network;

import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpParams;

import son.hut.vn.app.utility.SmartNetworkUtility;
import android.content.Context;
import android.os.AsyncTask;

/**
 * AsyncHttpBase is base class for AsyncHttpGet and AsyncHttpPost class
 * 
 * @author sonnv6207
 * 
 */
public class AsyncHttpBase extends AsyncTask<String, Integer, String>
{

    // Network status
    public static final int NETWORK_STATUS_OK = 0;

    public static final int NETWORK_STATUS_OFF = 1;

    public static final int NETWORK_STATUS_ERROR = 2;

    public static final int NETWORK_STATUS_TIMEOUT = 3;
    
    public static final int NETWORK_EXCEPTION = 4;
    
    public static String CAUSE_ERROR = "";
    
    protected Context context;

    protected AsyncHttpResponseListener listener;

    protected List<NameValuePair> parameters;

    protected HttpResponse response;

    protected int statusCode;

    /**
     * Constructor
     * 
     * @param context
     * @param listener
     * @param parameters
     */
    public AsyncHttpBase(Context context, AsyncHttpResponseListener listener,  List<NameValuePair> parameters)
    {
        this.context = context;
        this.listener = listener;
        this.parameters = parameters;
    }

    /*
     * @see android.os.AsyncTask#onPreExecute()
     */
    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
        listener.before();
    }

    /*
     * @see android.os.AsyncTask#doInBackground(Params[])
     */
    @Override
    protected String doInBackground(String... args)
    {
        if (SmartNetworkUtility.getInstance(context).isNetworkAvailable())
        {
            return request(args[0]);
        }
        else
        {
            // Return status if network is not available
            statusCode = NETWORK_STATUS_OFF;
            return null;
        }
    }

    /**
     * Send request to server
     * 
     * @param url
     * @return
     */
    protected String request(String url)
    {
        // This function will be implemented in AsyncHttpGet and AsyncHttpPost class
        return null;
    }
    
    /*
     * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
     */
    @Override
    protected void onPostExecute(String result)
    {
        // Call method to process http status code and response
        listener.after(statusCode, response);
    }
    
    // ============================================================================

    /**
     * Create HttpClient based on HTTP or HTTPS protocol that is parsed from url
     * parameter. With HTTPS protocol, we accept all SSL certificates.
     * 
     * @param url
     * @param params
     * @return
     */
    protected HttpClient createHttpClient(String url, HttpParams params)
    {
            // HTTP process
            return new DefaultHttpClient(params);
    }
  
  
}
